package com.cndongtai.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.net.ssl.HttpsURLConnection;

public class HttpsUtil {
	
	private static final String ENCODEING = "utf-8";
	private static final int CONNECT_TIMEOUT = 1000*1;
	private static final int SEND_TIMEOUT =1000*1;
	
	public static String doPost(String reqUrl, Map<String, String> parameters) {
		HttpsURLConnection url_con = null;
		String responseContent = null;
		
		try {
			StringBuffer params = new StringBuffer();
			for (Iterator<?> iter = parameters.entrySet().iterator(); iter.hasNext();) {
				Entry<?, ?> element = (Entry<?, ?>) iter.next();
				params.append(element.getKey().toString());
				params.append("=");
				params.append(URLEncoder.encode(element.getValue().toString(), ENCODEING));
				params.append("&");
			}

			if (params.length() > 0) {
				params = params.deleteCharAt(params.length() - 1);
			}

			URL url = new URL(reqUrl);
			url_con = (HttpsURLConnection) url.openConnection();
			url_con.setRequestMethod("POST");
			url_con.setConnectTimeout(CONNECT_TIMEOUT);
			url_con.setReadTimeout(SEND_TIMEOUT);
			url_con.setDoOutput(true);
			byte[] b = params.toString().getBytes();
			url_con.getOutputStream().write(b, 0, b.length);
			url_con.getOutputStream().flush();
			url_con.getOutputStream().close();

			InputStream in = url_con.getInputStream();
			byte[] echo = new byte[10 * 1024];
			int len = in.read(echo);
			responseContent = (new String(echo, 0, len, ENCODEING)).trim();
			int code = url_con.getResponseCode();
			if (code != 200) {
				responseContent = "ERROR" + code;
			}
		}catch (IOException e) {
			System.out.println("网络故障:"+ e.toString());
		}finally {
			if (url_con != null) {
				url_con.disconnect();
			}
		}
		return responseContent;
	}
	
	public static String doGet(String reqUrl, Map<String, String> parameters) {
		HttpsURLConnection url_con = null;
		String responseContent = null;
		
		try {
			StringBuffer params = new StringBuffer();
			for (Iterator<?> iter = parameters.entrySet().iterator(); iter.hasNext();) {
				Entry<?, ?> element = (Entry<?, ?>) iter.next();
				params.append(element.getKey().toString());
				params.append("=");
				params.append(URLEncoder.encode(element.getValue().toString(), ENCODEING));
				params.append("&");
			}

			if (params.length() > 0) {
				params = params.deleteCharAt(params.length() - 1);
			}

			URL url = new URL(reqUrl+"?"+params.toString());
			url_con = (HttpsURLConnection) url.openConnection();
			url_con.setRequestMethod("GET");
			url_con.setConnectTimeout(CONNECT_TIMEOUT);
			url_con.setReadTimeout(SEND_TIMEOUT);
			url_con.setDoOutput(true);

			InputStream in = url_con.getInputStream();
			byte[] echo = new byte[10 * 1024];
			int len = in.read(echo);
			responseContent = (new String(echo, 0, len, ENCODEING)).trim();
			int code = url_con.getResponseCode();
			if (code != 200) {
				responseContent = "ERROR" + code;
			}
		}catch (IOException e) {
			System.out.println("网络故障:"+ e.toString());
		}finally {
			if (url_con != null) {
				url_con.disconnect();
			}
		}
		return responseContent;
	}

}
